﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using Abp.Application.Services;
using Abp.Configuration.Startup;
using Abp.Modules;
using Abp.WebApi;
using Swashbuckle.Application;

namespace apbmvc
{
    [DependsOn(typeof(AbpWebApiModule), typeof(apbmvcApplicationModule))]
    public class apbmvcWebApiModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());

            Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
                .ForAll<IApplicationService>(typeof(apbmvcApplicationModule).Assembly, "app")
                       .WithConventionalVerbs() //这样我们就可以看到特定的HTTP前缀，不然全是post，HTTP动词是通过方法名的前缀决定的：
                .Build();

           ConfigureSwaggerUi();
        }

        private void ConfigureSwaggerUi()
        {
            Configuration.Modules.AbpWebApi().HttpConfiguration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "API文档");
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

                    //将application层中的注释添加到SwaggerUI中
                    var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

                    var commentsFileName = "bin//apbmvc.Application.xml";
                    var commentsFile = Path.Combine(baseDirectory, commentsFileName);
                    //将注释的XML文档添加到SwaggerUI中
                    c.IncludeXmlComments(commentsFile);

                })
              .EnableSwaggerUi(c => c.InjectJavaScript(Assembly.GetExecutingAssembly(), " apbmvc.SwaggerUI.Scripts.wagger.js"));
        }
        public override void PreInitialize()
        {
            Configuration.Modules.AbpWeb().AntiForgery.IsEnabled = false;

            base.PreInitialize();
        }
    }
}
